/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rw.magallanes.ide.commons.search;

import com.rw.magallanes.core.Item;
import com.rw.magallanes.utilities.MessageType;
import com.rw.magallanes.utilities.Utilities;
import com.rw.table.EditionStyle;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

/**
 *
 * @author Gonzalez
 */
public class SearchDialog extends javax.swing.JDialog {

    private SearchTableModel model;

    public SearchDialog(Window window) {
        super(window, "Buscar", ModalityType.APPLICATION_MODAL);
        initComponents();
        initValues();
    }

    private void initValues() {
        this.model = new SearchTableModel();
        this.table.setModel(model);
        this.model.configure(table, EditionStyle.EXCEL_STYLE);

        this.textSearch.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                String keyword = textSearch.getText();
                if (keyword != null && !keyword.isEmpty()) {
                    List<Item> search = SearchEngine.search(keyword);
                    model.setItems(search);
                } else {
                    model.setItems(null);
                }
                model.fireTableDataChanged();
            }
        });

        JPanel main = (JPanel) this.getContentPane();
        Utilities.configureAction(main, new CloseAction());
    }

    public void showMe() {
        this.setSize(500, 400);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    private class CloseAction extends AbstractAction {

        @Override
        public void actionPerformed(ActionEvent e) {
            dispose();
        }

        @Override
        public Object getValue(String key) {
            if (key.compareTo(NAME) == 0) {
                return "Cerrar";
            } else if (key.compareTo(ACTION_COMMAND_KEY) == 0) {
                return "close_search_dialog";
            } else if (key.compareTo(ACCELERATOR_KEY) == 0) {
                return KeyStroke.getKeyStroke("ESCAPE");
            } else {
                return super.getValue(key);
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelSearch = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        textSearch = new javax.swing.JTextField();
        buttonAdd = new javax.swing.JButton();
        panelCenter = new javax.swing.JPanel();
        scroll = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        panelButtons = new javax.swing.JPanel();
        buttonClose = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        panelSearch.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel1.setText("Buscar");
        panelSearch.add(jLabel1);

        textSearch.setColumns(30);
        panelSearch.add(textSearch);

        buttonAdd.setText("+");
        buttonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAddActionPerformed(evt);
            }
        });
        panelSearch.add(buttonAdd);

        getContentPane().add(panelSearch, java.awt.BorderLayout.NORTH);

        panelCenter.setLayout(new javax.swing.BoxLayout(panelCenter, javax.swing.BoxLayout.LINE_AXIS));

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        scroll.setViewportView(table);

        panelCenter.add(scroll);

        getContentPane().add(panelCenter, java.awt.BorderLayout.CENTER);

        panelButtons.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        buttonClose.setText("Cerrar");
        buttonClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCloseActionPerformed(evt);
            }
        });
        panelButtons.add(buttonClose);

        getContentPane().add(panelButtons, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCloseActionPerformed
        this.dispose();
    }//GEN-LAST:event_buttonCloseActionPerformed

    private void buttonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAddActionPerformed
        String name = this.textSearch.getText();

        if (name != null && !name.isEmpty()) {
            AddItemDialog dialog = new AddItemDialog(this, name);
            dialog.showMe();
        } else {
            Utilities.showMsg(this, "Nombre Invalido", MessageType.WARN);
        }

    }//GEN-LAST:event_buttonAddActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonAdd;
    private javax.swing.JButton buttonClose;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel panelButtons;
    private javax.swing.JPanel panelCenter;
    private javax.swing.JPanel panelSearch;
    private javax.swing.JScrollPane scroll;
    private javax.swing.JTable table;
    private javax.swing.JTextField textSearch;
    // End of variables declaration//GEN-END:variables
}
